package com.example.testandroid;

import java.util.Random;

import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;

import com.facebook.AppEventsLogger;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;

import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.doubleclick.AppEventListener;
import com.google.android.gms.ads.doubleclick.PublisherAdRequest;
import com.google.android.gms.ads.doubleclick.PublisherAdView;

public class AdsActivity extends Activity {
	private String LOG_TAG = "ads2";
	
	
	  /** Your ad unit id. Replace with your actual ad unit id. */
	  private static final String AD_UNIT_ID = "ca-app-pub-4105375449899778/5606132641";

	  /** The interstitial ad. */
	  private InterstitialAd interstitialAd;
	  
	  private boolean  isAdsReady = false;
	  
	  /** The view to show the ad. */
	  private PublisherAdView adView;
	  
	  private EditText _editMsg;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_ads);
		RelativeLayout layout = (RelativeLayout) findViewById(R.id.iadsLayout);
		_editMsg = (EditText)layout.findViewById(R.id.editAdsMsg);
		
		
		
		interstitialAd = new InterstitialAd(this);
        interstitialAd.setAdUnitId(AD_UNIT_ID);     
        interstitialAd.setAdListener(new LoggingAdListener());
        
		
	
		
		   adView = new PublisherAdView(this);
	       adView.setAdUnitId("ca-app-pub-4105375449899778/5215853049");
	       // adView.setAdSizes(AdSize.BANNER, AdSize.MEDIUM_RECTANGLE, new AdSize(120, 20));
	        
	       adView.setAdSizes(AdSize.BANNER);
	        //adView.setAdSizes(new AdSize(120, 20));
	        /** The implementation below is the same for all examples. */
	        // Set the AdListener to listen for standard ad events.
	        adView.setAdListener(new LoggingAdListener());
	       // adView.setPaddingRelative(500, 300, 150, 10);
	                
	        layout.addView(adView,
	        		 new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
	
	        
	        adView.loadAd(new PublisherAdRequest.Builder()
			.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
			.build());
	        
	        loadInterstitial();
			
		
	}
	
    /** Called when the Load Interstitial button is clicked. */
    public void loadInterstitial() {    
  //.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
      // Check the logcat output for your hashed device ID to get test ads on a physical device.
     
    	if (interstitialAd != null)
    	{
	    	AdRequest adRequest = new AdRequest.Builder()
	      	.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
	          .addTestDevice("INSERT_YOUR_HASHED_DEVICE_ID_HERE")
	          .build();
	
	      // Load the interstitial ad.
	      interstitialAd.loadAd(adRequest);
    	}
    }
    
    /** Called when the Show Interstitial button is clicked. */
    public void showInterstitial() {
      if (interstitialAd != null && interstitialAd.isLoaded()) {
    	  Log.d(LOG_TAG, "ad showing.");
    	  Random r = new Random();
    	  int i1 = r.nextInt(10);
    	  if (i1 < 2)
    	  {
    		  _editMsg.setText(R.string.agreeAds5);
    		  PlayActivity._currentScore += 5;    		  
    	  }
    	  else if (i1 < 5)
    	  {
    		  _editMsg.setText(R.string.agreeAds2);
    		  PlayActivity._currentScore += 2;
    	  }
    	  else
    	  {
    		  _editMsg.setText(R.string.agreeAds1);
    		  PlayActivity._currentScore += 1;
    	  }
        interstitialAd.show();
      } else {
        Log.d(LOG_TAG, "Interstitial ad was not ready to be shown.");
      }
    }

    /** Gets a string error reason from an error code. */
    private String getErrorReason(int errorCode) {
      String errorReason = "";
      switch(errorCode) {
        case AdRequest.ERROR_CODE_INTERNAL_ERROR:
          errorReason = "Internal error";
          break;
        case AdRequest.ERROR_CODE_INVALID_REQUEST:
          errorReason = "Invalid request";
          break;
        case AdRequest.ERROR_CODE_NETWORK_ERROR:
          errorReason = "Network Error";
          break;
        case AdRequest.ERROR_CODE_NO_FILL:
          errorReason = "No fill";
          break;
      }
      return errorReason;
    }
    
    
    
    /** Called when the refresh button is clicked. */
    public void refreshAd() {
      if (adView != null) {
        adView.loadAd(new PublisherAdRequest.Builder().build());
      }
    }
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		//getMenuInflater().inflate(R.menu.ads, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	   @Override
	    protected void onResume() {
	        super.onResume();

	        if (adView != null) {
	            adView.resume();
	          }
	        loadInterstitial(); 
	       // showInterstitial();
	    }
	   
	   @Override
	    public void onPause() {
	    	
	    	 if (adView != null) {
	             adView.pause();
	           }
	         
	        super.onPause();
	     

	        // Call the 'deactivateApp' method to log an app event for use in analytics and advertising
	        // reporting.  Do so in the onPause methods of the primary Activities that an app may be launched into.
	       
	        AppEventsLogger.deactivateApp(this);
	    }

	    @Override
	    public void onDestroy() {
	    	
	    	  if (adView != null) {
	              adView.destroy();
	            }
	        super.onDestroy();
	     
	    }
	    
	    
		public void exit(View v)
		{
			setResult(RESULT_OK);
			finish();
		}
		
		public void showInter(View v)
		{
			showInterstitial(); 
		}
	    
	    
	    

	 


	    
	
}
